package com.ch04.ex38;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;

/**
 * Created by zc on 2016/2/18.
 */
public class Main {
    public static void main(String[] args){
        ExecutorService executor = Executors.newCachedThreadPool();

        List<Task> taskList = new ArrayList<>();
        for(int i=0; i<3; i++){
            Task task = new Task(String.valueOf(i));
            taskList.add(task);
        }

        List<Future<Result>> resultList = null;

        try {
            resultList = executor.invokeAll(taskList);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        executor.shutdown();

        System.out.printf("Main: Printing the results\n");
        for(int i=0; i<resultList.size(); i++){
            Future<Result> future = resultList.get(i);
            try {
                Result result = future.get();
                System.out.println(result.getName() +": " + result.getValue());
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }

        }
    }
}
